#!/bin/bash
#
# N.B.: Execute checktest by  "bash ./checktest" instead of just "./checktest", because
# "./checktest" may not be executable on all operating systems.

if [ ! $# -eq 2 ] ; then
	printf "Usage: checktest -w|--warning|-e|--error|-we|--warnerror LOGFILE\n"
	exit 2
fi

LOGFILE="$2"

WARNINGS="$(grep -i "warning\|Remark\|Questionable\|attention\|F[0-9]*-W\|F[0-9]*-I" "$LOGFILE" \
    | grep -vi "WARNING --  When --chk x is specified" \
    | grep -vi "[0-9]\s*warning" \
    | grep -vi "in __debug_mod_MOD_warning" \
    | grep -vi "forrtl: warning (406): fort: (1): In call to WHATDAY, an array temporary was created for argument #1" \
    | grep -vi "MAXFILT is too small" \
    | grep -vi "Only the history of the last [0-9]* iteration(s) is recorded" \
    | grep -vi "circle\.f90(36): remark #7712: This variable has not been used\.\s*\[ANGLE\]" \
    | grep -vi "warningid\|warningmsg" \
    | grep -vi "Warning: TRSTEP: Assertion failed: D is initialized." \
	| grep -vi "Warning: UOBYQA: \|Warning: NEWUOA: \|Warning: BOBYQA: \|Warning: LINCOA: \|Warning: COBYLA: " \
    | grep -vi "invalid maxfun\|invalid maxhist\|invalid npt\|invalid rhobeg\|invalid rhoend\|invalid ftarget\|invalid ctol\|invalid maxfilt\|errstop")"

ERRORS="$(grep -i "error\|fail\|exception\|invalid\|bad\|overflow\|segmentation\|fault\|illegal\|F[0-9]*-S\|F[0-9]*-F\|F[0-9]*-E\|Could\ not\ resolve\|not\ defined\|Cannot connect to licence server (error 113)" "$LOGFILE" \
    | grep -vi "default" \
    | grep -vi "[0-9]\s*error" \
    | grep -vi "[0-9a-z]bad\|bad[0-9a-z]" \
    | grep -vi "[0-9a-z]fail" \
    | grep -vi "Warning: [A-Z]*MS.*G: Failed to open file .*_output" \
    | grep -vi "Warning:\ Floating\ overflow\ occurred" \
    | grep -vi "precaution against overflow" \
    | grep -vi "precaution against rounding error" \
    | grep -vi "Warning:\ Floating\ invalid\ operation\ occurred" \
    | grep -vi "Processor does not support trapping of floating-point exceptions" \
    | grep -vi "Assertion failed: Q interpolates FVAL at XPT" \
    | grep -vi "Assertion failed: H = W^{-1} in (3.12) of the NEWUOA paper" \
    | grep -vi "Assertion failed: BETA_TEST == BETA" \
    | grep -vi "Assertion failed: SUM(VLAG(1:NPT)) == 1" \
    | grep -vi "flist:.*: Invalid format" \
    | grep -vi "BAD_TRSTEP" \
    | grep -vi "moderr\|errorid\|errormsg\|errstop" \
    | grep -vi "berr\|erres" \
    | grep -vi "Warning: TRSTEP: Assertion failed: D is initialized." \
	| grep -vi "Warning: UOBYQA: Invalid\|Warning: NEWUOA: Invalid\|Warning: BOBYQA: Invalid\|Warning: LINCOA: Invalid\|Warning: COBYLA: Invalid" \
    | grep -vi "invalid maxfun\|invalid maxhist\|invalid npt\|invalid rhobeg\|invalid rhoend\|invalid ftarget\|invalid ctol\|invalid maxfilt\|errstop")"

case "$1" in
	-w|--warning)
        if [ -n "$WARNINGS" ]; then
            printf "\n\n-----------------------------------------------------------------------\n"
            printf "| Warnings have been found in %s:\n\n" "$LOGFILE"
            sed "s/^/| /" "$LOGFILE"
            printf "\n|-----------------------------------------------------------------------\n\n"
            printf "***********************************************************************\n"
            printf "* Warnings logged in %s:\n\n" "$LOGFILE"
            printf "%s" "$(echo "$WARNINGS" | sed "s/^/* /")"
            printf "\n***********************************************************************\n\n"
		fi
		exit 0
		;;
	-e|--error)
        if [ -n "$ERRORS" ]; then
            printf "\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
            printf "+ Errors have been found in %s:\n\n" "$LOGFILE"
            sed "s/^/+ /" "$LOGFILE"
            printf "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n"
            printf "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
            printf "X Errors logged in %s:\n\n" "$LOGFILE"
            printf "%s" "$(echo "$ERRORS" | sed "s/^/X /")"
            printf "\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n\n"
			exit 1
		else
			exit 0
		fi
		;;
	-we|--warnerror)
        if [ -n "$ERRORS" ]; then
            printf "\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
            printf "+ Errors have been found in %s:\n\n" "$LOGFILE"
            sed "s/^/+ /" "$LOGFILE"
            printf "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n"
            printf "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
            printf "X Errors logged in %s:\n\n" "$LOGFILE"
            printf "%s" "$(echo "$ERRORS" | sed "s/^/X /")"
            printf "\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n\n"
        elif [ -n "$WARNINGS" ]; then
            printf "\n\n-----------------------------------------------------------------------\n"
            printf "| Warnings have been found in %s:\n\n" "$LOGFILE"
            sed "s/^/| /" "$LOGFILE"
            printf "\n|-----------------------------------------------------------------------\n\n"
            printf "***********************************************************************\n"
            printf "* Warnings logged in %s:\n\n" "$LOGFILE"
            printf "%s" "$(echo "$WARNINGS" | sed "s/^/* /")"
            printf "\n***********************************************************************\n\n"
        fi
        if [ -n "$WARNINGS" ] || [ -n "$ERRORS" ]; then
            exit 1
		else
			exit 0
		fi
		;;
	*)
		exit 3
		;;
esac
